﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using BicycleTrafficSimulator.DataModel;

namespace BicycleTrafficSimulator.Presentation
{
    class InstructionPage : DrawableGameComponent
    {
        private Scene scene;

        private Texture2D backgroundTexture;

        private Vector2 readyButtonPosition;
        private Vector2 exitButtonPosition;

        private Rectangle readyButtonRect;

        public Rectangle ReadyButtonRect
        {
            get { return readyButtonRect; }
            set { readyButtonRect = value; }
        }
        private Rectangle exitButtonRect;

        public Rectangle ExitButtonRect
        {
            get { return exitButtonRect; }
            set { exitButtonRect = value; }
        }

        MouseState mouseState;
        MouseState previousMouseState;

        public InstructionPage(Scene _scene)
            : base(_scene)
        {
            scene = _scene;
        }

        public override void Initialize()
        {
            base.Initialize();

            readyButtonPosition = new Vector2(180, 650);
            exitButtonPosition = new Vector2(700, 640);

            readyButtonRect = new Rectangle((int)readyButtonPosition.X, (int)readyButtonPosition.Y, 250, 70);
            exitButtonRect = new Rectangle((int)exitButtonPosition.X, (int)exitButtonPosition.Y, 250, 70);

            mouseState = Mouse.GetState();
            previousMouseState = mouseState;

            backgroundTexture = scene.Content.Load<Texture2D>("instructionpage");
        }

        public override void Update(GameTime gameTime)
        {

            mouseState = Mouse.GetState();
            if (previousMouseState.LeftButton == ButtonState.Pressed &&
               mouseState.LeftButton == ButtonState.Released)
            {
                MouseClicked(mouseState.X, mouseState.Y);
            }

            previousMouseState = mouseState;
            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            scene.SpriteBatchMenu.Begin();
            scene.SpriteBatchMenu.Draw(backgroundTexture, Vector2.Zero, Color.White);
            scene.SpriteBatchMenu.End();
            base.Draw(gameTime);
        }

        void MouseClicked(int x, int y)
        {
            //creates a rectangle of 10x10 around the place where the mouse was clicked
            Rectangle mouseClickRect = new Rectangle(x, y, 10, 10);

            if (mouseClickRect.Intersects(ReadyButtonRect)) 
            {
                scene.GameStateScene = Scene.GameState.StartMenu;
            }
            else if (mouseClickRect.Intersects(ExitButtonRect)) 
            {
                scene.Exit();
            }
        }
    }
}

